package com.example.safeassistan.home.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.safeassistan.R;
import com.example.safeassistan.base.BaseActivity;
import com.example.safeassistan.constants.UrlConstants;
import com.example.safeassistan.hiddendanger.model.DepartMentAllModel;
import com.example.safeassistan.home.adapter.AuthorizedepartmentAdapter;
import com.example.safeassistan.http.HttpRequest;
import com.example.safeassistan.http.HttpStringCallBack;
import com.example.safeassistan.utile.JsonUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class AuthorizeDepartMentPerson extends BaseActivity implements View.OnClickListener {
    private AuthorizedepartmentAdapter adapter;
    private RecyclerView rcView;
    private EditText etEdit;
    private TextView tvSearch;
    private String defectId="";
    private String name="";

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, AuthorizeDepartMentPerson.class);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.authorize_department;
    }

    @Override
    protected void initView() {
        setTitleText("人员选择");
        defectId=getIntent().getStringExtra("defectId");
        rcView=findViewById(R.id.rcCommon);
        etEdit=findViewById(R.id.etEdit);
        tvSearch=findViewById(R.id.tvSearch);
        tvSearch.setOnClickListener(this);
        etEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                name=s.toString().trim();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        getData();
    }

    @Override
    protected void initData() { }

    @Override
    protected int getTitleBarType() {
        return 0;
    }

    public void getData(){
        Map map=new HashMap<>();
        if (!defectId.equals("")){
            map.put("defectId",defectId);
            if (!name.equals("")){
                map.put("userName",name);
            }
        }
        new HttpRequest(this).doGet(UrlConstants.getPersonByDepartId, null,map, new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    Boolean success = jsonObject.getBoolean("success");
                    if (success) {
                        Object obj = JsonUtils.fromJson(result.toString(), DepartMentAllModel.class);
                        if (obj instanceof DepartMentAllModel) {
                            DepartMentAllModel model=(DepartMentAllModel) obj;
                            adapter=new AuthorizedepartmentAdapter(model.getResult());
                            rcView.setLayoutManager(new LinearLayoutManager(getContext()));
                            rcView.setAdapter(adapter);
                            adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                @Override
                                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                    Intent intent = new Intent();
                                    intent.putExtra("id",model.getResult().get(position).getId());
                                    intent.putExtra("name",model.getResult().get(position).getName());
                                    setResult(RESULT_OK, intent);
                                    finish();
                                }
                            });
                        }
                    }else {
                        Toast.makeText(AuthorizeDepartMentPerson.this,message,Toast.LENGTH_SHORT).show();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {

            }
        });
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tvSearch:
                getData();
                break;
        }
    }
}
